/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Implements the visible top section of the app. This includes implementing
 *             the controller aspect of switching main views via the menu widget.           
 */

package Decision_Lists;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;


/**
 */
public class HeaderView extends JPanel implements DL_ViewInterface {

  //////////////////////////////////
  ///// Class Vars /////////////////
  boolean is_center_view = false;
  DL_Model model;
  final String view_name = "Header";
  int comboBoxWidth = 200;
  int comboBoxHeight = 28;
  //int panelHeightPref = 78;  // If using 2 lines (logo on top line, list-name/combobox on 2nd)
  int panelHeightPref = 52;  // If everything on one line
  Color panel_BG_Color = new Color(240, 243, 236);  // grayish-warm-greenish

  JLabel listNameLabel, listWordLabel, viewLabel, changeLabel, decisionLabel, listLabel;
  JComboBox viewComboBox;
  JSeparator separator_logo_horiz;

  Font font_plainHelv11 = new Font("Helvetica", Font.PLAIN, 10);
  Font font_plainHelv18 = new Font("Helvetica", Font.PLAIN, 16);
  Font font_boldHelv18  = new Font("Helvetica", Font.BOLD,  16);
  Font font_boldHelv20  = new Font("Helvetica", Font.BOLD,  18);
  Font font_plainHelv24 = new Font("Helvetica", Font.PLAIN, 22);
  Font font_boldHelv24  = new Font("Helvetica", Font.BOLD,  22);

  DL_ViewInterface addItemView;
  
  ///// end Class Vars /////////////
  //////////////////////////////////

  // Constructor
  public HeaderView(final DL_Model model, DL_ViewInterface addItemView) {
    this.model = model;           // link the model to this view
    this.model.addObserver(this, this.view_name, is_center_view); // link this view to the model

    setBackground(panel_BG_Color);
    this.addItemView = addItemView;

    ////////////////////////////////
    //// Make Text Labels ////////// (combo box must be made later, after views have registered)

    // "DecisionLists" logo labels
    decisionLabel = new JLabel("Decision");
    listLabel     = new JLabel("List: ");
    decisionLabel.setFont(font_plainHelv24);
    listLabel.setFont(    font_boldHelv24 );
    listLabel.setForeground(Color.gray);
            // decisionLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    // Separator below Logo
    separator_logo_horiz = new JSeparator(SwingConstants.HORIZONTAL);
    separator_logo_horiz.setForeground(new Color(140,140,140, 150));  // rgb a (out of 255)
    // "List: " label
    listWordLabel = new JLabel("List: ", SwingConstants.LEFT);
    listWordLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    listWordLabel.setFont(font_plainHelv18);
    // List Name Label (i.e., "Houses to Buy")
    listNameLabel = new JLabel(model.getListName(), SwingConstants.LEFT);
    listNameLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    //listNameLabel.setFont(font_boldHelv20);
    listNameLabel.setFont(font_plainHelv24);
    // "change list" label
    changeLabel   = new JLabel("change list", SwingConstants.LEFT);
    changeLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    changeLabel.setFont(font_plainHelv11);
    changeLabel.setForeground(Color.gray);

  } ////////// end constructor /////////


  // Layout this header panel.  Includes all center views in the drop down. This gets called in it's
  //  container class after all other views have been instantiated (thus this layout cannot happen
  //  in the constructor).
  public void layout_my_panel() {

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    // put a 10 pixel, empty border around the panel
    //setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    setBorder(BorderFactory.createEmptyBorder(10,10,0,10));  // Top, Left, Btm, Right
    ////////////////////////////////////////
    ////////// ADD TEXT LABELS /////////////
    // Logo "DecisionLists"
    Box logoBox = Box.createHorizontalBox();
    logoBox.add(Box.createRigidArea(new Dimension(10, 0))); // a little padding on left of logo

    decisionLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    listLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);

    logoBox.add(decisionLabel); // "Decision"
    logoBox.add(listLabel);     // "Lists"
            //    logoBox.add(Box.createHorizontalGlue());  // Make logo appear on far left
            //    add(logoBox);

    listWordLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);

    logoBox.add(listNameLabel);  // "Houses to Buy"
    // "change lists"
    logoBox.add(Box.createRigidArea(new Dimension(6, 0)));
    logoBox.add(changeLabel);

    logoBox.add(Box.createHorizontalGlue());
    logoBox.add(Box.createHorizontalGlue());
    logoBox.add(Box.createHorizontalGlue());
    logoBox.add(Box.createHorizontalGlue());
    
    // "View: "
    viewLabel = new JLabel("View: ");
    viewLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    viewLabel.setFont(font_plainHelv18);
    logoBox.add(viewLabel);

    // View Combo Box
    viewComboBox = createViewMenu(); // must be done here, after all other views are created
    viewComboBox.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    logoBox.add(viewComboBox);
    logoBox.add(Box.createHorizontalGlue());

    add(logoBox);

  // <editor-fold defaultstate="collapsed" desc="When using 2 lines for header">
  /*
    // Separator below Logo
    add(Box.createRigidArea(new Dimension(0, 3)));
    add(separator_logo_horiz);
    add(Box.createRigidArea(new Dimension(0, 3)));

    // Horiz Box to hold "List: Houses to Buy .... View: Butterfly"
    Box listname_viewname_box = Box.createHorizontalBox();
    listname_viewname_box.add(Box.createHorizontalGlue());
    // "List: Houses to Buy"
    Box listNameBox = Box.createHorizontalBox();
    listNameBox.add(listWordLabel);
    listNameBox.add(listNameLabel);
    listname_viewname_box.add(listNameBox);
    // "change lists"
    listname_viewname_box.add(Box.createRigidArea(new Dimension(6, 0)));
    listname_viewname_box.add(changeLabel);

    listname_viewname_box.add(Box.createHorizontalGlue());
    listname_viewname_box.add(Box.createHorizontalGlue());
    listname_viewname_box.add(Box.createHorizontalGlue());
    listname_viewname_box.add(Box.createHorizontalGlue());
    // "View: "
    viewLabel = new JLabel("View: ");
    viewLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    viewLabel.setFont(font_plainHelv18);
    listname_viewname_box.add(viewLabel);

    // View Combo Box
    viewComboBox = createViewMenu();
    viewComboBox.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    listname_viewname_box.add(viewComboBox);
    listname_viewname_box.add(Box.createHorizontalGlue());

    //listname_viewname_box.setAlignmentY(Component.TOP_ALIGNMENT);  // can't get this to work
    //listname_viewname_box.setMaximumSize(new Dimension(400, 28));
    //add(Box.createRigidArea(new Dimension(0, -35)));
    //listname_viewname_box.setBackground(Color.red);
    add(listname_viewname_box);
    //listname_viewname_box.setAlignmentY(Component.TOP_ALIGNMENT);
     */// </editor-fold>
  } ////////// end layout_panel() /////////

  
  ///////////////////////////////////////////////////////////////////////////////
  // Makes view DROP DOWN MENU (must be made after all views have registered )/////////
  private JComboBox createViewMenu() {

    ArrayList <String> center_view_names_list = this.model.getCenterViewNames();

    JComboBox cb = new JComboBox(center_view_names_list.toArray());
    cb.setSelectedItem(CenterPanel3.getCurrentCenterView().getViewName());

    // EVENT HANDLER for when a new center-view selection has been made
    cb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        // Turn the string name of the view into the view object and send it to the model.
        model.setCurView(CenterPanel3.views_map.get((String)cb.getSelectedItem()));
      }
    });
        //cb.setSize(comboBoxWidth, comboBoxHeight);

    // This command doesn't seem to actually set the size, but it at least makes the comboBox
    //  be a reasonable size (rather than taking up entire height of the container).
    cb.setMaximumSize(new Dimension(comboBoxWidth, comboBoxHeight));
    cb.setFont(font_boldHelv18);
    //cb.setBackground(Color.lightGray);
    // <editor-fold defaultstate="collapsed" desc="cb.setRenderer()...">
    /*
    cb.setRenderer(new DefaultListCellRenderer() {
        @Override
        public void paint(Graphics g) {
        setBackground(Color.lightGray);
        setForeground(Color.BLACK);
        super.paint(g);
      }
    });  */// </editor-fold>  // Tried to set the combo box's selected bg color (but then it wouldn't give a hover indication
    
    return cb;
  } ////////////// end createViewMenu() /////////////////


  /////////////////////////////////////////////////
  ////// Methods Required by the Interface ////////

  // the Model calls this to notify this View of a changed current value
  public void curViewChanged(String old_view_name, String new_view_name ) {
    if( old_view_name.equals(this.view_name) ) {
 
    }

    if(new_view_name.equals(addItemView.getViewName())) {

      //System.out.println("HeaderView notices that addItemView is now the view.");
      viewLabel.setVisible(false);
      viewComboBox.setVisible(false);
    }
    else {
      viewComboBox.setVisible(true);
      viewLabel.setVisible(true);
    }
  } ///// end curViewChanged() //////////

  // Model calls this to inform views of an added (new) list item.
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemAdded(List_Item_Data newItem) {
    // Do nothing
  }  
  // Model calls this to inform views of a changed list item (i.e., new item added, old item deleted, etc).
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemChanged() {
    // Do nothing, this view doesn't care about new list items
  }

  public String getViewName() {
    return this.view_name;
  } ///////

  /////// end required by interface functions  //////
  ///////////////////////////////////////////////////




  @Override
  public Dimension getMinimumSize()   { return getPreferredSize(); }
  @Override  // TODO: set the width as the width of the parent
  public Dimension getPreferredSize() { return new Dimension(500, panelHeightPref); } // Width ignored for NORTH 
  
} ////////////// end class HeaderView /////////////////////////
